function add0(m) {
  return m < 10 ? "0" + m : m;
}
function format(shijianchuo) {
  //shijianchuo是整数，否则要parseInt转换
  var time = new Date(shijianchuo);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  return y + "-" + add0(m) + "-" + add0(d);
}
const holiday = {
  "2018-12-30": "2018-12-28",
  "2018-12-31": "2018-12-28",
  "2019-01-01": "2018-12-28",
  "2019-02-04": "2019-02-01",
  "2019-02-05": "2019-02-01",
  "2019-02-06": "2019-02-01",
  "2019-02-07": "2019-02-01",
  "2019-02-08": "2019-02-01",
  "2019-02-09": "2019-02-01",
  "2019-02-10": "2019-02-01",
  "2019-04-05": "2019-04-04",
  "2019-04-06": "2019-04-04",
  "2019-04-07": "2019-04-04",
  "2019-05-01": "2019-04-30",
  "2019-06-07": "2019-06-06",
  "2019-06-08": "2019-06-06",
  "2019-06-09": "2019-06-06",
  "2019-09-13": "2019-09-12",
  "2019-09-14": "2019-09-12",
  "2019-09-15": "2019-09-12",
  "2019-10-01": "2019-09-28",
  "2019-10-02": "2019-09-28",
  "2019-10-03": "2019-09-28",
  "2019-10-04": "2019-09-28",
  "2019-10-05": "2019-09-28",
  "2019-10-06": "2019-09-28",
  "2019-10-07": "2019-09-28",
  "2020-01-01": "2019-12-31",
  "2020-01-24": "2020-01-23",
  "2020-01-25": "2020-01-23",
  "2020-01-26": "2020-01-23",
  "2020-01-27": "2020-01-23",
  "2020-01-28": "2020-01-23",
  "2020-01-29": "2020-01-23",
  "2020-01-30": "2020-01-23",
  "2020-04-04": "2020-04-03",
  "2020-04-05": "2020-04-03",
  "2020-04-06": "2020-04-03",
  "2020-05-01": "2020-04-30",
  "2020-05-02": "2020-04-30",
  "2020-05-03": "2020-04-30",
  "2020-06-25": "2020-06-24",
  "2020-06-26": "2020-06-24",
  "2020-06-27": "2020-06-24",
  "2020-10-01": "2020-09-30",
  "2020-10-02": "2020-09-30",
  "2020-10-03": "2020-09-30",
  "2020-10-04": "2020-09-30",
  "2020-10-05": "2020-09-30",
  "2020-10-06": "2020-09-30",
  "2020-10-07": "2020-09-30"
};

const timeChange = time => {
  var preDate = time ? time : new Date(); //传入日期||当前日期

  const date = new Date();
  const hour = date.getHours(); //小时
  const day = date.getDate(); //一个月中的一天
  const preday = preDate.getDate(); //一个月中的一天

  const oneDay = 24 * 60 * 60 * 1000; //一天
  if (hour < 18 && preday == day) {
    preDate = new Date(date.getTime() - oneDay); //前一天
  }

  const pre = format(preDate);
  if (holiday[pre]) return new Date(holiday[pre]).getTime(); //19年节假日

  const week = preDate.getDay(); //周几
  if (week == 0 || week == 6) {
    preDate = week == 0 ? preDate.getTime() - 2 * oneDay : preDate.getTime() - oneDay; //周六日取周五的数据
  } else {
    preDate = preDate.getTime();
  }

  return preDate;
};

export default timeChange;
